package com.andova.esgl.sample

import android.opengl.GLES30
import android.opengl.GLSurfaceView
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.andova.egl.VBO
import java.lang.reflect.Parameter
import java.nio.ByteBuffer
import javax.microedition.khronos.egl.EGLConfig
import javax.microedition.khronos.opengles.GL10

class VBOActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val glsv = GLSurfaceView(this)
        setContentView(glsv)
        glsv.setEGLContextClientVersion(3)
        glsv.setRenderer(VBORender())
    }
}

class VBORender : GLSurfaceView.Renderer {
    var pixelBuffer: ByteBuffer? = null

    /**
     * glReadPixels 数组是像素的大小 而不是像素分量的大小 故用 IntBuffer
     */
    private fun readPixels(parameter: Parameter? = null) {
        val imgW = 4
        val imgH = 4
        if (pixelBuffer == null) pixelBuffer = ByteBuffer.allocate(imgW * imgH * 3)
        GLES30.glReadPixels(0, 0, imgW, imgH, GLES30.GL_RGB, GLES30.GL_UNSIGNED_BYTE, pixelBuffer)
        val colors = pixelBuffer?.array()
        println()
    }

    override fun onDrawFrame(gl: GL10?) {
        VBO.drawFrame()
        readPixels()
    }

    override fun onSurfaceCreated(gl: GL10?, config: EGLConfig?) = run { VBO.surfCreated() }
    override fun onSurfaceChanged(gl: GL10?, width: Int, height: Int) = run { VBO.surfChanged(width, height) }
}